package com.gavchap2d.starfield;

import android.content.SharedPreferences;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Handler;
import android.service.wallpaper.WallpaperService;
import android.view.SurfaceHolder;

/*
 * This animated wallpaper draws a rotating wireframe cube.
 */
public class StarfieldWallpaper extends WallpaperService {

    private final Handler mHandler = new Handler();
    public final static String SHARED_PREFS_NAME = "starfield_prefs";

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public Engine onCreateEngine() {
        return new StarfieldEngine();
    }

    class StarfieldEngine extends Engine implements SharedPreferences.OnSharedPreferenceChangeListener  {

        private final Paint mPaint = new Paint();
        private SharedPreferences mPrefs;
        private String direction;

        private final Runnable mDrawStars = new Runnable() {
            public void run() {
                drawFrame();
            }
        };
        private boolean mVisible;
        final int nStars=100;
        double stars_x[]=new double[nStars];
        double stars_y[]=new double[nStars];
        double stars_s[]=new double[nStars];
        float ox=0;
        float oy=0;
        float scale=1;
        
        StarfieldEngine() {
            // Create a Paint to draw the lines for our cube
            final Paint paint = mPaint;
            paint.setColor(0xffffffff);
            paint.setAntiAlias(true);
            paint.setStrokeWidth(1);
            paint.setStrokeCap(Paint.Cap.ROUND);
            paint.setStyle(Paint.Style.FILL);
            
            mPrefs = StarfieldWallpaper.this.getSharedPreferences(SHARED_PREFS_NAME, 0);
            mPrefs.registerOnSharedPreferenceChangeListener(this);
            onSharedPreferenceChanged(mPrefs, null);
        }
        
        public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {

            direction = prefs.getString("starfield_direction", "left");
            
            // read the 3D model from the resource
            //readModel(shape);
        }

        @Override
        public void onCreate(SurfaceHolder surfaceHolder) {
            super.onCreate(surfaceHolder);
            int i;
            for(i=0;i<nStars;i++)
            {
            	stars_x[i]=Math.random()*this.getDesiredMinimumWidth();
            	stars_y[i]=Math.random()*this.getDesiredMinimumHeight();
            	stars_s[i]=2+Math.random()*8;
            	
            }
            scale = getResources().getDisplayMetrics().density;

        }

        @Override
        public void onDestroy() {
            super.onDestroy();
            mHandler.removeCallbacks(mDrawStars);
        }

        @Override
        public void onVisibilityChanged(boolean visible) {
            mVisible = visible;
            if (visible) {
                drawFrame();
            } else {
                mHandler.removeCallbacks(mDrawStars);
            }
        }

        @Override
        public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
            super.onSurfaceChanged(holder, format, width, height);
        }

        @Override
        public void onSurfaceCreated(SurfaceHolder holder) {
            super.onSurfaceCreated(holder);
        }

        @Override
        public void onSurfaceDestroyed(SurfaceHolder holder) {
            super.onSurfaceDestroyed(holder);
            mVisible = false;
            mHandler.removeCallbacks(mDrawStars);
        }

        @Override
        public void onOffsetsChanged(float xOffset, float yOffset,
                float xStep, float yStep, int xPixels, int yPixels) {
        	ox=xPixels;
        	oy=yPixels;
            //drawFrame();
        }


         void drawFrame() {
            final SurfaceHolder holder = getSurfaceHolder();

            Canvas c = null;
            try {
                c = holder.lockCanvas();
                if (c != null) {
                    // draw something
                    drawStars(c);
                  
                }
            } finally {
                if (c != null) holder.unlockCanvasAndPost(c);
            }

            mHandler.removeCallbacks(mDrawStars);
            if (mVisible) {
                mHandler.postDelayed(mDrawStars, 1000 / 30);
            }
        }


        void drawStars(Canvas c) {
            c.save();
            c.drawColor(0xff000000);
           
            direction=mPrefs.getString("starfield_direction", "left");
            //Toast.makeText(getApplicationContext(), direction, Toast.LENGTH_SHORT).show();
            for(int i=0; i<nStars; i++)
            {
            	if(direction.equals("left"))
            		stars_x[i]+=stars_s[i];
            	if(direction.equals("right"))
            		stars_x[i]-=stars_s[i];
            	if(direction.equals("up"))
            		stars_y[i]-=stars_s[i];
            	if(direction.equals("down"))
            		stars_y[i]+=stars_s[i];
               	
            	if(stars_x[i]>this.getDesiredMinimumWidth())
            		stars_x[i]=0;
            	if(stars_x[i]<0)
            		stars_x[i]=this.getDesiredMinimumWidth();             
            	
            	if(stars_y[i]>this.getDesiredMinimumHeight())
            		stars_y[i]=0;
            	if(stars_y[i]<0)
            		stars_y[i]=this.getDesiredMinimumHeight();
            	//mPaint.setShader(new LinearGradient((float) stars_x[i]+ox, (float) stars_y[i]+oy,(float) stars_x[i]+ox-((5*scale)), (float) stars_y[i]+oy,   0x00FFFFFF, 0xffffffff, Shader.TileMode.MIRROR));
            	c.drawCircle((float) stars_x[i]+ox, (float) stars_y[i]+oy, 1*scale, mPaint);
            	//c.drawLine((float) stars_x[i]+ox, (float) stars_y[i]+oy,(float) stars_x[i]+ox-((10*scale)), (float) stars_y[i]+oy,mPaint);
            }
            

            c.restore();
        }



    }
}